﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ARA.WebApp
{
    public partial class AnalyseByDate : System.Web.UI.Page
    {
        public string SummaryHtml { get; set; }

        int _leaveEarlierCount;
        int _joinLateCount;
        int _neglectCount;
        int _normalCount;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ctlBtnSubmit_Click(object sender, EventArgs e)
        {
            Guid batchId = new Guid(Request.QueryString["batchId"]);
            DateTime date = Convert.ToDateTime(ctlEditDate.Text);

            DateTime beginDate = new DateTime(date.Year, date.Month, date.Day, 0, 0, 0);
            DateTime endDate = new DateTime(date.Year, date.Month, date.Day, 23, 59, 59);

            List<ARA.Domain.Datas.Person> personList = new ARA.Domain.Services.AnalyseService().AnalyseByDateRange(batchId, beginDate, endDate);

            StringBuilder builder = new StringBuilder();
            BuildResultHeader(builder, beginDate, endDate);
            BuildResultBody(builder, personList, beginDate, endDate);
            BuildResultFooter(builder);
            ctlOutput.InnerHtml = builder.ToString();

            float total = _joinLateCount + _leaveEarlierCount + _neglectCount + _normalCount;
            SummaryHtml += "<label>迟到</label>" + _joinLateCount.ToString() + "<label>比例:</label>" + Math.Round(((float)_joinLateCount / total) * 100.0f, 2).ToString() + "%";
            SummaryHtml += "&nbsp;&nbsp;|&nbsp;&nbsp;<label>早退</label>" + _leaveEarlierCount.ToString() + "<label>比例:</label>" + Math.Round(((float)_leaveEarlierCount / total) * 100.0f, 2).ToString() + "%";
            SummaryHtml += "&nbsp;&nbsp;|&nbsp;&nbsp;<label>旷工</label>" + _neglectCount.ToString() + "<label>比例:</label>" + Math.Round(((float)_neglectCount / total) * 100.0f, 2).ToString() + "%";
            SummaryHtml += "&nbsp;&nbsp;|&nbsp;&nbsp;<label>正常</label>" + _normalCount.ToString() + "<label>比例:</label>" + Math.Round(((float)_normalCount / total) * 100.0f, 2).ToString() + "%";
        }

        private void BuildResultHeader(StringBuilder stringBuilder, DateTime beginDate, DateTime endDate)
        {
            stringBuilder.Append("<table border='0' cellspacing='0' cellpadding='0' ><tr><th>Name</th>");
            int deltaDay = (endDate - beginDate).Days;
            for (int index = 0; index <= deltaDay; index++)
            {
                stringBuilder.AppendFormat("<th>{0}</th>", beginDate.AddDays(index).ToString("MM-dd"));
            }
            stringBuilder.Append("</tr>");
        }

        private void BuildResultFooter(StringBuilder stringBuilder)
        {
            stringBuilder.Append("</table>");
        }

        private void BuildResultBody(StringBuilder stringBuilder, List<ARA.Domain.Datas.Person> personList, DateTime beginDate, DateTime endDate)
        {
            for (int pIndex = 0; pIndex < personList.Count; pIndex++)
            {
                ARA.Domain.Datas.Person person = personList[pIndex];
                stringBuilder.Append("<tr>");
                stringBuilder.AppendFormat("<td>{0}({1})</td>", person.Account, person.Name);

                int deltaDay = (endDate - beginDate).Days;
                for (int index = 0; index <= deltaDay; index++)
                {
                    ARA.Domain.Datas.DayRecord dayRecord = person.GetDayRecord(beginDate.AddDays(index));
                    if (dayRecord == null)
                    {
                        stringBuilder.Append("<td>-</td>");
                    }
                    else
                    {
                        string status = null;
                        if (dayRecord.JoinTime == null)
                        {
                            status = "[旷]";
                            _neglectCount++;
                        }
                        else
                        {
                            if (dayRecord.IsLate())
                            {
                                status = "[迟]";
                                _joinLateCount++;
                            }
                            if (dayRecord.IsEarlier())
                            {
                                status += "[退]";
                                _leaveEarlierCount++;
                            }

                            if (status == null)
                            {
                                status = "[正]";
                                _normalCount++;
                            }
                        }
                        stringBuilder.AppendFormat("<td>{0}</td>", status);
                    }
                }
                stringBuilder.Append("</tr>");
            }
        }
    }
}